using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeWork.Library.Web;
using EasyCommerce.BLL;
using CodeWork.Library;
using System.Drawing;

public partial class public_Recover : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitControls();
    }

    private void InitControls()
    {
        ibtSendPassword.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/recoverPassword.png";
        if (string.IsNullOrEmpty(Request["account"]))
        {
            pnlSendRecoverMail.Visible = true;
            pnlChangePassword.Visible = false;
        }
        else
        {
            string userId = Request["account"];
            string question = UserManager.GetSecurityQuestion(userId);
            if(!string.IsNullOrEmpty(question))
                lblSecurityQuestion.Text = question;

            pnlSendRecoverMail.Visible = false;
            pnlChangePassword.Visible = true;        
        }
    }

    protected void SendPassword_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            if (UserManager.RecoverPassword(tbxUserId.Text))
            {
                lblRecoverMessage.Text = CommonMessage.RecoverSuccessMessage;
                lblRecoverMessage.ForeColor = Color.Green;
            }
            else
            {
                lblRecoverMessage.Text = CommonMessage.RecoverErrorMessage;
                lblRecoverMessage.ForeColor = Color.Red;
                
            }
            lblRecoverMessage.Visible = true;
        }
    }

    protected void ChangePassword_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string userId = Request["account"];
            string confirmationCode = Request["confirmationCode"];

            if (UserManager.ChangePasswordForPasswordRecovery(userId, tbxSecurityAnswer.Text,
                confirmationCode, uccPassword.Password))
            {
                lblChangePasswordMessage.Text = CommonMessage.PasswordChangeSuccessMessage;
                lblChangePasswordMessage.ForeColor = Color.Green;
            }
            else
            {
                lblChangePasswordMessage.Text = CommonMessage.PasswordChangeErrorMessage;
                lblChangePasswordMessage.ForeColor = Color.Red;
            }
            lblChangePasswordMessage.Visible = true;
        }
    }
}
